package com.weiyuwqj.server.service.asset.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.BasAssetsPackage;
import com.weiyuwqj.api.model.BusAssetPackageInvest;
import com.weiyuwqj.api.model.BusAssetPackagePlan;
import com.weiyuwqj.api.model.BusUserStat;
import com.weiyuwqj.api.model.TicketParamEntity;
import com.weiyuwqj.api.model.vo.BasUserInviteVo;
import com.weiyuwqj.api.model.vo.BusAssetPackageInvestVo;
import com.weiyuwqj.api.service.BusAssetPackageInvestService;
import com.weiyuwqj.api.util.StringUtils;
import com.weiyuwqj.server.mapper.BasAssetsPackageMapper;
import com.weiyuwqj.server.mapper.BasUserInviteMapper;
import com.weiyuwqj.server.mapper.BusAssetPackageInvestMapper;
import com.weiyuwqj.server.mapper.BusAssetPackagePlanMapper;
import com.weiyuwqj.server.mapper.BusUserStatMapper;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.service.invest.module.AssetInvestModule;
import com.weiyuwqj.server.service.sys.module.ConfigTicketModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2017/3/29.
 */
@Service("assetPackageInvestService")
public class BusAssetPackageInvestServiceImpl extends BaseServiceImpl<BusAssetPackageInvest> implements BusAssetPackageInvestService {

    @Autowired
    private BusAssetPackagePlanMapper packagePlanMapper;

    @Autowired
    private AssetInvestModule assetInvestModule;

    @Autowired
    private BusUserStatMapper statMapper;

    @Autowired
    private ConfigTicketModule ticketModule;

    @Autowired
    private BasUserInviteMapper userInviteMapper;

    @Autowired
    private BasAssetsPackageMapper assetsPackageMapper;
    @Autowired
    private BusAssetPackageInvestMapper assetPackageInvestMapper;


//    /**
//     * 债权投资
//     * @param userId        用户ID
//     * @param share        投资金额
//     * @param packagePlanId 项目ID
//     * @param investType    投资类型
//     */
//    @Override
//    public Map doInvest(Integer userId, Integer share, Integer packagePlanId, Integer investType) {
//        BusAssetPackagePlan plan = packagePlanMapper.selectByPrimaryKey(packagePlanId);
//        BasAssetsPackage basAssetsPackage = assetsPackageMapper.selectByPrimaryKey(plan.getAssetPackageId());
//        BigDecimal amount = basAssetsPackage.getEachAmount().multiply(new BigDecimal(share));
//        List<Map> list = assetInvestModule.doInvest(userId, amount, plan, investType);
//
//
//        Map statMap = new HashMap();
//        statMap.put("userId", userId);
//        BusUserStat stat = statMapper.getOneByUserId(statMap);
//
//        if (stat != null && stat.getInvestCount() == 0) {
//            // 首次投资个人奖励
//            TicketParamEntity firstInvestParam = new TicketParamEntity();
//            firstInvestParam.setNid(Constants.first_invest_nid);
//            firstInvestParam.setUserId(userId);
//            ticketModule.addTicket(firstInvestParam);
//
//            //首次投资邀请人奖励
//            BasUserInviteVo userInvite = userInviteMapper.getOneByInviteId(userId);
//            if (userInvite != null) {
//                TicketParamEntity firstInvesInvitetParam = new TicketParamEntity();
//                firstInvesInvitetParam.setNid(Constants.first_invite_invest_nid);
//                firstInvesInvitetParam.setUserId(userInvite.getUserId());
//                firstInvesInvitetParam.setAmount(amount);
//                ticketModule.addTicket(firstInvesInvitetParam);
//            }
//        }
//
//        //单笔投资满额奖励
//        TicketParamEntity investParam = new TicketParamEntity();
//        investParam.setNid(Constants.invest_nid);
//        investParam.setUserId(userId);
//        investParam.setAmount(amount);
//        ticketModule.addTicket(investParam);
//
//        //总投资满额奖励
//        TicketParamEntity totalInvestParam = new TicketParamEntity();
//        totalInvestParam.setNid(Constants.total_invest_nid);
//        totalInvestParam.setUserId(userId);
//        ticketModule.addTicket(totalInvestParam);
//        return Message.OK;
//    }

    /**
     * 根据计划id查询计划投资列表
     * @param packagePlanId
     * @return
     */
    @Override
    public PageList getListByPlanId(Paginator p, Integer packagePlanId) {
        PageHelper.startPage(p.getPageNum(),p.getPageSize());
        List<BusAssetPackageInvestVo> list = assetPackageInvestMapper.getListByPlanId(packagePlanId);
        //转义手机号码为*
        for(BusAssetPackageInvestVo assetPackageInvestVo:list){
            assetPackageInvestVo.setMobile(StringUtils.getString(assetPackageInvestVo.getMobile()));
        }
        return new PageList(list);
    }

}
